//
//  ViewController.swift
//  Swift-TomCat
//
//  Created by 李龙 on 15/3/8.
//  Copyright (c) 2015年 Lauren. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    ///tom猫View
    @IBOutlet weak var tomView: UIImageView!
      
    var dict :NSDictionary?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //从plist中获取
        let path = NSBundle.mainBundle().pathForResource("tom.plist", ofType: nil)
        println(path)
        dict = NSDictionary(contentsOfFile: path!)
        println(dict)
    }


    @IBAction func animOnClick(sender: UIButton) {
        if tomView.isAnimating() {
            return
        }
        
        
        let title = sender.currentTitle
        let count = dict![title!] as! Int
        
        var animArray = [UIImage]()
        
        
        for i in 0..<count{
//            let imageName = "\(title!)_\(i).jpg"
            let imageName = String(format: "\(title!)_%02d.jpg", arguments: [i])
            let path = NSBundle.mainBundle().pathForResource(imageName, ofType: nil)
            println(path)
            var img = UIImage(contentsOfFile: path!)
            animArray.append(img!)
        }
        
        //3.给Tom设置动画图片
        tomView.animationImages = animArray
        //4.设置播放时间
        tomView.animationDuration =  Double(count) * 0.1
        //5.设置播放次数
        tomView.animationRepeatCount = 1
        //6.开始播放
        tomView.startAnimating()

    }
    
    
    
    

}

